<?php

namespace App\Http\Controllers;

use App\AccessTokens;
use App\Members;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;


    // 签名验证
    protected function checkSign($sign){
        if(empty($sign)){
            return ['status'=>false,'msg' =>'sign error'];
        }
        $sign = explode('-', $sign);
        if(count($sign) != 2){return ['status'=>false,'msg' =>'sign error'];}

        $token = AccessTokens::where('token',$sign[1])->first();
        if(empty($token)){return ['status'=>false,'msg' =>'sign error'];}
        $signMd5 = md5($token['token'].$token['time']);
        if($signMd5 != $sign[0]){return ['status'=>false,'msg' =>'sign error'];}
        // 验证成功则删除
        AccessTokens::where('token',$sign[1])->delete();
        return ['status'=>true];
    }

    //验证用户合法性
    public function checkUser($uid,$random){
        if(empty($uid) || empty($random)){
           return false;
        }

        $user = Members::where('u_id',$uid)->where('u_random',$random)->first()->toArray();
        if(empty($user)){
            return false;
        }
        return $user;
    }
}
